<?php
namespace app\administrative\controller;

use app\common\model\MenuModel;
use think\Controller;
use think\Request;
use app\common\model\Notice;
use app\common\model\Customer;
use app\common\model\Engineers;
use app\common\model\Operation;
use app\common\model\Administrative;
use app\common\model\WorkOrder;



class Index extends Base
{
    public function index(){
        return $this->fetch('/index');
    }
    
    
    
    /**
     * 查询子菜单栏
     */
    
    public function MenuList(){
        if(request()->isPost()){
            $meun = new MenuModel();
            $id = input("param.id");
            $data = $meun->getSubmenu(['pid'=>$id], $this->set['auth_rule']);
            if(session($this->set['db_name'].'is_admin')!=1){
                $rule =  explode(',',session($this->set['db_name'].'rule'));
                foreach ($rule as $key => $val){
                    foreach ($data as $k => $v){
                        if(intval($v['id']) == intval($val)){
                            $arr[] = $v;
                        }
                    }
                }
                $data = $arr;
            }
            return json(['code'=>1,'msg'=>'','data'=>$data]);
        }
    }
    
    
    /**
     * 首页
     */
    public function doIndex(){
        //公告
        $notice = new Notice();
        $noticeInfo = $notice->order('update_time desc')->find();
        $this->view->noticeInfo = $noticeInfo;

        /*
         *  操作台数据
         *  李港
         */
        $a_id = session($this->set['db_name'].'a_id');
        //今日新增用户数
        $Customer = new Customer();
        $userWhere = [
            'rq_customer_administrative_center.administrative_id'=>$a_id
        ];
        $userTime = ['add_time'=>'today'];
        $userJoin = [
            ['rq_customer_administrative_center','rq_customer_administrative_center.user_id = rq_customer.user_id']
        ];
        $New_subscribers_today = $Customer->CountUserNumber($userWhere,$userTime,$userJoin);
        //现有用户总数
        $Total_number_existing_users = $Customer->CountUserNumber($userWhere,[],$userJoin);

        //今日新增工程人员数
        $Engineers = new Engineers();
        $engineersWhere = [
            'administrative_id'=>$a_id
        ];
        $engineersTime = ['reg_time'=>'today'];
        $engineersJoin = [];
        $new_engineers_number_today = $Engineers->CountEngineersNumber($engineersWhere,$engineersTime,$engineersJoin);
        //现有工程人员总数
        $Total_number_existing_engineers = $Engineers->CountEngineersNumber($engineersWhere,[],$engineersJoin);

        //今日完成工单
        $WorkOrder = new WorkOrder();
        //今日完成工单总数
        $db_table = 'rq_work_order';
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $whereTimeFiled = 'update_time';
        $whereTimeOp = 'today';
        $Total_work_order_completed_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //新装工单数
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>3,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Finish_the_new_job_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //换芯工单数
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>2,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Finish_the_change_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //续费工单数/总金额
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>1,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Today_the_completion_of_renewal_orders = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>1,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Total_Money = $WorkOrder->countWorkOrderMoney($WorkOrderWhere,$db_table,'combo_money',[],$whereTimeFiled,$whereTimeOp);
        $Total_Money = number_format($Total_Money,2);
        //移机工单数
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>5,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $The_singular_shift_completed_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //维修工单数
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>6,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Completed_number_maintenance_workers_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //拆机工单数
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>4,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Complete_teardown_orders_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //续费+维修工单数
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>7,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Today_completion_maintenance_renewal_singular = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //换芯+维修工单数
        $WorkOrderWhere = [
            'work_order_status'=>12,
            'business_id'=>8,
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $Completed_today_change_core_repair_singular = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);

        //今日未完成工单
        //今日完成未工单总数
        $db_table = 'rq_work_order';
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'is_delete'=>0
        ];
        $whereTimeFiled = 'update_time';
        $whereTimeOp = 'today';
        $Total_work_order_not_completed_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //新装工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>3,
            'is_delete'=>0
        ];
        $Today_singular_number_new_workers_not_completed = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //换芯工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>2,
            'is_delete'=>0
        ];
        $Today_odd_worker_not_completed = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //续费工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>1,
            'is_delete'=>0
        ];
        $Today_not_complete_renewal_orders = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //移机工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>5,
            'is_delete'=>0
        ];
        $Today_singular_shift_not_complete = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //维修工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>6,
            'is_delete'=>0
        ];
        $Incomplete_number_repairman_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //拆机工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>4,
            'is_delete'=>0
        ];
        $Complete_not_teardown_orders_today = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //续费+维修工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>7,
            'is_delete'=>0
        ];
        $Today_not_completion_maintenance_renewal_singular = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);
        //换芯+维修工单数
        $WorkOrderWhere = [
            'work_order_status'=>[['>',1],['<',12]],
            'administrative_id'=>$a_id,
            'business_id'=>8,
            'is_delete'=>0
        ];
        $Completed_not_today_change_core_repair_singular = $WorkOrder->getCount($WorkOrderWhere,$db_table,'','',$whereTimeFiled,$whereTimeOp);

        $data = [
            //统计板块
            'New_subscribers_today'=>$New_subscribers_today,
            'Total_number_existing_users'=>$Total_number_existing_users,
            'new_engineers_number_today'=>$new_engineers_number_today,
            'Total_number_existing_engineers'=>$Total_number_existing_engineers,
            //今日完成板块
            'Total_work_order_completed_today'=>$Total_work_order_completed_today,
            'Finish_the_new_job_today'=>$Finish_the_new_job_today,
            'Finish_the_change_today'=>$Finish_the_change_today,
            'Today_the_completion_of_renewal_orders'=>$Today_the_completion_of_renewal_orders,
            'Total_Money'=>$Total_Money,
            'The_singular_shift_completed_today'=>$The_singular_shift_completed_today,
            'Completed_number_maintenance_workers_today'=>$Completed_number_maintenance_workers_today,
            'Complete_teardown_orders_today'=>$Complete_teardown_orders_today,
            'Today_completion_maintenance_renewal_singular'=>$Today_completion_maintenance_renewal_singular,
            'Completed_today_change_core_repair_singular'=>$Completed_today_change_core_repair_singular,
            //今日未完成板块
            'Total_work_order_not_completed_today'=>$Total_work_order_not_completed_today,
            'Today_singular_number_new_workers_not_completed'=>$Today_singular_number_new_workers_not_completed,
            'Today_odd_worker_not_completed'=>$Today_odd_worker_not_completed,
            'Today_not_complete_renewal_orders'=>$Today_not_complete_renewal_orders,
            'Today_singular_shift_not_complete'=>$Today_singular_shift_not_complete,
            'Incomplete_number_repairman_today'=>$Incomplete_number_repairman_today,
            'Complete_not_teardown_orders_today'=>$Complete_not_teardown_orders_today,
            'Today_not_completion_maintenance_renewal_singular'=>$Today_not_completion_maintenance_renewal_singular,
            'Completed_not_today_change_core_repair_singular'=>$Completed_not_today_change_core_repair_singular,
        ];
        $this->assign('data',$data);
        return $this->fetch('index/index');
    }
    
    
}


